<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Identifies an element's attributes</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="simplexmlelement.asxml.html">« SimpleXMLElement::asXML</a></li>
      <li style="float: right;"><a href="simplexmlelement.children.html">SimpleXMLElement::children »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.simplexmlelement.html">SimpleXMLElement</a></li>
    <li>Identifies an element's attributes</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="simplexmlelement.attributes" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SimpleXMLElement::attributes</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SimpleXMLElement::attributes</span> &mdash; <span class="dc-title">Identifies an element&#039;s attributes</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-simplexmlelement.attributes-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SimpleXMLElement::attributes</strong></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">string</span><span class="type"></span></span> <code class="parameter">$namespaceOrPrefix</code><span class="initializer"> = <strong><code>null</code></strong></span></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$isPrefix</code><span class="initializer"> = <strong><code>false</code></strong></span></span>): <span class="type"><span class="type">?</span><span class="type"><a href="class.simplexmlelement.html" class="type SimpleXMLElement">SimpleXMLElement</a></span></span></div>

  <p class="para rdfs-comment">
   This function provides the attributes and values defined within an xml tag.
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">SimpleXML
建起了一个给大多数方法添加迭代属性的规则。不能通过使用
<span class="function"><a href="function.var-dump.html" class="function">var_dump()</a></span> 或任何可检查对象的其它东西来查看。</span></p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-simplexmlelement.attributes-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">namespaceOrPrefix</code></dt>

     <dd>

      <p class="para">
       An optional namespace for the retrieved attributes
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">isPrefix</code></dt>

     <dd>

      <p class="para">
       Default to <strong><code>false</code></strong>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-simplexmlelement.attributes-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a <span class="classname"><a href="class.simplexmlelement.html" class="classname">SimpleXMLElement</a></span> object that can be
   iterated over to loop through the attributes on the tag.
  </p>
  <p class="para">
   Returns <strong><code>null</code></strong> if called on a <span class="classname"><a href="class.simplexmlelement.html" class="classname">SimpleXMLElement</a></span>
   object that already represents an attribute and not a tag.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-simplexmlelement.attributes-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5453">
    <p><strong>示例 #1 Interpret an XML string</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$string&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;XML<br /></span><span style="color: #DD0000">&lt;a&gt;<br />&nbsp;&lt;foo&nbsp;name="one"&nbsp;game="lonely"&gt;1&lt;/foo&gt;<br />&lt;/a&gt;<br /></span><span style="color: #007700">XML;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">simplexml_load_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br />foreach(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">attributes</span><span style="color: #007700">()&nbsp;as&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #DD0000">'="'</span><span style="color: #007700">,</span><span style="color: #0000BB">$b</span><span style="color: #007700">,</span><span style="color: #DD0000">"\"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
name=&quot;one&quot;
game=&quot;lonely&quot;
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-simplexmlelement.attributes-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="simplexml.examples-basic.html" class="xref">Basic SimpleXML usage</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>